package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.CountDownButtonHelper;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.PermissionsChecker;
import com.diudiutechnology.www.yadiudiu.util.utils.PictureUtil;
import com.diudiutechnology.www.yadiudiu.util.utils.SelectPhotoPop;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyApplication;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * Created by Administrator on 2017/8/25.
 * 添加银行卡
 */

public class AddBankCardActivity extends BaseActivity implements View.OnClickListener, Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.the_cardholder_text)//持卡人
            EditText the_cardholder_text;

    @Bind(R.id.id_number_text)//身份证号
            EditText id_number_text;

    @Bind(R.id.id_card_is)//身份证正面
            ImageView id_card_is;

    @Bind(R.id.id_card_back)//身份证反面
            ImageView id_card_back;

    @Bind(R.id.card_number_text)//卡号
            EditText card_number_text;

    @Bind(R.id.reserved_phone_text)//预留手机
            EditText reserved_phone_text;

    @Bind(R.id.verification_code_text)//验证码
            EditText verification_code_text;


    @Bind(R.id.admin_btn_yzm)//获取验证码
            Button admin_btn_yzm;

    @Bind(R.id.button)//添加
            Button button;

    private MyProgressDialog mDialog;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    int idcardis = 0;//身份证正面 0、可点击
    int idcardback = 0;//身份证反面 0、可点击
    String Judge_interface = "0";//判断哪接口  0、短信 1、添加 2、添加身份证
    String Judge = "0";//判断哪图片  0、身份证正面  1、身份证反面
    Context context;
    private SelectPhotoPop selectPhotoPop;
    private View contentView;
    MineFragmentModel model;//缓存数据
    String conimg = "";//图片路径
    String justimg = "";//身份证正面图片路径
    String backimg = "";//身份证反面图片路径

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = View.inflate(this, R.layout.activity_add_bank_card,
                null);
        setContentView(contentView);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        id_card_is.setOnClickListener(this);
        id_card_back.setOnClickListener(this);
        admin_btn_yzm.setOnClickListener(this);
        button.setOnClickListener(this);
        options = ImgUtils.imageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        initData();
    }

    private void initData() {
        model = UserInfoUtils.readUserInfo(this);

        //持卡人
        if (!Sundry.getStinges(model.getRealname())) {
            the_cardholder_text.setText(model.getRealname());
            the_cardholder_text.setEnabled(false);
        }
        //卡号
        if (!Sundry.getStinges(model.getIdcardno())) {
            id_number_text.setText(model.getIdcardno());
            id_number_text.setEnabled(false);
        }
        //身份证正面
        if (!Sundry.getStinges(model.getFrontofidcard())) {
            justimg = model.getFrontofidcard();
            imageLoader.displayImage(Constant.HTTP + model.getFrontofidcard(), id_card_is, options);
            idcardis = 1;
        }
        //身份证反面
        if (!Sundry.getStinges(model.getIdcardnegative())) {
            backimg = model.getIdcardnegative();
            imageLoader.displayImage(Constant.HTTP + model.getIdcardnegative(), id_card_back, options);
            idcardback = 1;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.admin_btn_yzm://短信
                if (TextUtils.isEmpty(reserved_phone_text.getText().toString().trim())) {
                    ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                    return;
                }
                if (!Sundry.isMobileNO(reserved_phone_text.getText().toString().trim())) {//验证手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_correctness_phone_number));
                    return;
                }
                Judge_interface = "0";
                processadd();
                break;
            case R.id.button:
                if (TextUtils.isEmpty(the_cardholder_text.getText().toString().trim())) {//持卡人
                    ToastUtils.show(context, getResources().getString(R.string.please_cardholder));
                    return;
                }
                if (TextUtils.isEmpty(id_number_text.getText().toString().trim())) {//身份证号
                    ToastUtils.show(context, getResources().getString(R.string.please_id_number));
                    return;
                }
                if (!Sundry.personIdValidation(id_number_text.getText().toString())) {//正确的身份证号
                    ToastUtils.show(this, getResources().getString(R.string.please_correct_id_number));
                    return;
                }
                if (Sundry.getStinges(justimg)) {//身份证正面
                    ToastUtils.show(context, getResources().getString(R.string.please_front));
                    return;
                }
                if (Sundry.getStinges(backimg)) {    //身份证反面
                    ToastUtils.show(context, getResources().getString(R.string.please_reverse));
                    return;
                }
                if (TextUtils.isEmpty(card_number_text.getText().toString().trim())) {//卡号
                    ToastUtils.show(context, getResources().getString(R.string.please_number));
                    return;
                }
                if (TextUtils.isEmpty(reserved_phone_text.getText().toString().trim())) {//手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                    return;
                }
                if (!Sundry.isMobileNO(reserved_phone_text.getText().toString().trim())) {//验证手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_correctness_phone_number));
                    return;
                }

                if (TextUtils.isEmpty(verification_code_text.getText().toString().trim())) {//验证码
                    ToastUtils.show(context, getResources().getString(R.string.please_verification_code));
                    return;
                }
                Judge_interface = "1";
                processadd();
                break;
            case R.id.id_card_is://正面
                if (idcardis == 0) {
                    if (!PermissionsChecker.getPermissionsChecker(this, "0", 0)) {
                        if (!PermissionsChecker.getPermissionsChecker(this, "0", 2)) {
                            Judge_interface = "2";
                            Judge = "0";
                            showSelectPicPop();

                        } else {
                            ToastUtils.show(context, "请检查是否打开储存权限");
                        }
                    } else {
                        ToastUtils.show(context, "请检查是否打开相机权限");
                    }
                }
                break;
            case R.id.id_card_back://反面
                if (idcardback == 0) {
                    if (!PermissionsChecker.getPermissionsChecker(this, "0", 0)) {
                        if (!PermissionsChecker.getPermissionsChecker(this, "0", 2)) {
                            Judge_interface = "2";
                            Judge = "1";
                            showSelectPicPop();
                        } else {
                            ToastUtils.show(context, "请检查是否打开储存权限");
                        }
                    } else {
                        ToastUtils.show(context, "请检查是否打开相机权限");
                    }
                }
                break;
                         /* 注释  相机*/
            case R.id.item_popupwindows_camera:
                selectPhotoPop.dismiss();
                GalleryFinal.openCamera(Constant.CAMERA, MyApplication.getInstance().initGalley(Constant.CAMERA, 1), mOnHanlderResultCallback);
                break;
            /* 注释  相册*/
            case R.id.item_popupwindows_Photo:
                selectPhotoPop.dismiss();
                GalleryFinal.openGallerySingle(Constant.GALLERYSINGLE, MyApplication.getInstance().initGalley(Constant.GALLERYSINGLE, 1), mOnHanlderResultCallback);
                break;
            /* 注释  取消*/
            case R.id.item_popupwindows_cancel:
                selectPhotoPop.dismiss();
                break;
        }
    }

    //相册
    private void showSelectPicPop() {
        selectPhotoPop = new SelectPhotoPop(this, this);
        selectPhotoPop.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
    }

    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            Bitmap bitmap = PictureUtil.decodeSampledBitmapFromFile(resultList.get(0).getPhotoPath(), 400, 400);
            conimg = PictureUtil.saveBitmapFile(bitmap, String.valueOf(0));
            processadd();
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {

        }
    };

    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote != null || getnote.size() > 0) {
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")) {
                if (Judge_interface.equals("0")) {//验证码
                    // 验证码
                    CountDownButtonHelper helper = new CountDownButtonHelper(admin_btn_yzm, 120, 1);
                    helper.start();
                    ToastUtils.show(context, getMsg);
                } else if (Judge_interface.equals("1")) {//添加银行卡
                    UserInfoUtils.getInstans().updatefrontofidcard(context, justimg);
                    UserInfoUtils.getInstans().updateIdcardnegative(context, backimg);
                    UserInfoUtils.getInstans().updatebankNumber(context, "1");
                    idcardis = 1;
                    idcardback = 1;
                    the_cardholder_text.setEnabled(false);
                    id_number_text.setEnabled(false);
                    ToastUtils.show(context, getMsg);
                } else if (Judge_interface.equals("2")) {//身份证
                    String content = getnote.get(0).getContent();
                    if (Judge.equals("0")) {//正面
                        justimg = content;
                        imageLoader.displayImage(Constant.HTTP + content, id_card_is, options);
                    } else {//反面
                        backimg = content;
                        imageLoader.displayImage(Constant.HTTP + content, id_card_back, options);
                    }
                    ToastUtils.show(context, getMsg);
                }
            } else if (status.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            } else {
                ToastUtils.show(context, getMsg);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }

    //添加银行卡 和短信
    private void processadd() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            if (Judge_interface.equals("0")) {
                HttpClient.note(reserved_phone_text.getText().toString().trim(), "5", this);
            } else if (Judge_interface.equals("1")) {
                HttpClient.addBankCard(the_cardholder_text.getText().toString().trim(), card_number_text.getText().toString().trim(),
                        reserved_phone_text.getText().toString().trim(), verification_code_text.getText().toString().trim(),
                        id_number_text.getText().toString().trim(), this);
            } else if (Judge_interface.equals("2")) {
                String str = "justimg";
                if (Judge.equals("0")) {//正面
                    str = "justimg";
                } else {//反面
                    str = "backimg";
                }
                HttpClient.editIdcardimg(str, conimg, this);
            }
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
}

